package org.wicketskunkworks.demo;

import java.util.Arrays;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.RangeValidator;
import org.wicketskunkworks.gravatar.GravatarImage;

/**
 * Homepage
 */
public class ShoeboxGravatarPage extends WebPage {
	private static final long serialVersionUID = 1L;

	private String email = "test@example.com";
	private Integer imageSize = 80;
	private String imageFallback = "";

	private String[] imageFallbackChoices = {
	        null, "identicon", "monsterid", "wavatar"
	};

	/**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
	public ShoeboxGravatarPage(final PageParameters parameters) {

		Form<ShoeboxGravatarPage> form = new Form<ShoeboxGravatarPage>(
		        "emailForm", new CompoundPropertyModel<ShoeboxGravatarPage>(
		                this)) {

			@Override
			protected void onSubmit() {
				String email = getModelObject().getEmail();
				Integer size = getModelObject().getImageSize();
				String fallback = getModelObject().getImageFallback();
				if ("".equals(fallback)) {
					fallback = null;
				}

				// System.err.println("fallback: " + fallback);
				// System.err.println("size: " + size);
				// System.err.println("email: " + email);

				GravatarImage child = new GravatarImage("gravatarImage", email,
				        true);
				child.setSize(size);
				child.setFallback(fallback);
				replace(child);
			}

		};
		form.add(new FeedbackPanel("feedbackPanel"));
		form.add(new RequiredTextField<String>("email"));

		TextField<Integer> imageSizeInput = new TextField<Integer>("imageSize");
		imageSizeInput.add(new RangeValidator<Integer>(1, 512));
		form.add(imageSizeInput);

		DropDownChoice<String> fallbackChoice = new DropDownChoice<String>(
		        "imageFallback", Arrays.asList(imageFallbackChoices),
		        new IChoiceRenderer<String>() {

			        @Override
			        public Object getDisplayValue(String object) {
				        if (object == null) {
					        return "None";
				        }
				        return object;
			        }

			        @Override
			        public String getIdValue(String object, int index) {
				        if (object == null) {
					        return "";
				        }
				        return object;
			        }
		        });
		form.add(fallbackChoice);

		form.add(new GravatarImage("gravatarImage", "brill@pappin.ca", true));
		add(form);

	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Integer getImageSize() {
		return imageSize;
	}

	public void setImageSize(Integer imageSize) {
		this.imageSize = imageSize;
	}

	public String getImageFallback() {
		return imageFallback;
	}

	public void setImageFallback(String imageFallback) {
		this.imageFallback = imageFallback;
	}

}
